package com.maosy.jenkins.servlet

import com.google.gson.Gson
import com.maosy.jenkins.api.UserService
import com.maosy.jenkins.services.UserServicesImpl
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

class UserLoginApi : HttpServlet() {

    private val gson: Gson = Gson()

    override fun doGet(req: HttpServletRequest?, resp: HttpServletResponse?) {
        this.doPost(req, resp)
    }

    override fun doPost(req: HttpServletRequest?, resp: HttpServletResponse?) {

        val userService: UserService = UserServicesImpl()
        val users = userService.login(req?.getParameter("userName") ?: "",
                req?.getParameter("password") ?: "")

        resp?.apply {
            writer.print(gson.toJson(users))
        }
    }
}
